<?php if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!=""){?>
<div class="col-md-8">
<div class="main-content" id="main-content">
	<div class="sub-content">
			<div class="top-header page-header clearfix">
				<h1>Categories</h1>
			</div><br/>
		<div class="user-profile1 col-md-12">
				<form class="form-horizontal" role="form">
				
						<div class="form-group">
						<label class="control-label col-sm-3">Category Name:</label>
						<div class="col-sm-9">
						<input type="text" id="cat_name"  name="cat_name" class="form-control" value="<? if(isset($this->get_category->category_name)){ echo $this->get_category->category_name;}?>" placeholder="Category name"/>
						</div></div>
						<div class="adding_list form-group">
							<div class="botton_btns">
								<div class="col-sm-3">&nbsp;</div>
								<div class="col-sm-9">
								<a href="javascript:void();" id="cat_btn" name="cat_btn" onclick="return catValidate();" class="btn btn-danger" role="button">Submit</a>
								<a href="<? echo $this->Url('products')?>/all-categories-list" class="btn btn-danger" role="button">Back</a></div>
						
							</div>
						</div>
					
			    	
				</form>
				<input type="hidden" id="hid_category_id" name="hid_category_id" value="<? if(isset($this->get_category->category_id)){ echo $this->get_category->category_id;}?>">
				<input type="hidden" id="hid_check" name="hid_check" value="<? if(isset($this->get_category->category_name)){ echo $this->get_category->category_name;}else{echo "0";}?>"/>
		 </div>
     </div>
</div></div>
<script>
   function catValidate(){
		var cat_name=$("#cat_name").val();
		var category_id=$("#hid_category_id").val();
		 var check_ind_name=$("#hid_check").val();
		if(cat_name==""){
			alert("Category name is required");return false;
		}
		if(category_id=='' || check_ind_name.toLowerCase()!=cat_name.toLowerCase()){
			$.ajax({
				type:'POST',
				url:BASE_PATH+'/products/check-category-name',
				data:{cat_name:cat_name},
				success: function(data){
					if(data.output=='notsuccess'){	
						$.ajax({
							type:'POST',
							url:BASE_PATH+'/add-category',
							data:{cat_name:cat_name,category_id:category_id},
							success: function(data){
								if(data.output=='success'){	
								  window.location=BASE_URL+'/products/all-categories-list';	
								}else{
								  alert('Not successfully added');
								}
							}
						});
					}else if(data.output=='success'){
						alert('Entered category is already exists');return false;
					}
				}
			});
		}else{
	window.location=BASE_URL+'/products/all-categories-list';
	}
}
</script>
</html>
<?php }else{?>
<script>
	window.location='<? echo $this->Url('home')?>';
</script>
<?php }?>



